package sample.tangocho.activity;

import java.util.List;

import sample.tangocho.model.Card;
import sample.tangocho.service.FindCardService;
import sample.tangocho.service.FindCardServiceImpl;
import sample.tangocho.service.RemoveCardService;
import sample.tangocho.service.RemoveCardServiceImpl;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

/**
 * カード閲覧アクティビティ
 *
 * @author
 *
 */
public class ViewCardActivity extends Activity {

	private int tangochoId;

	private List<Card> cardList;

	private FindCardService findCardService;

	private RemoveCardService removeCardService;

	private boolean frontFlag;

	private int index;

	private boolean flipperFlag = true;

	private ViewFlipper viewFlipper;
	private Animation inFromRightAnimation;
	private Animation inFromLeftAnimation;
	private Animation outToRightAnimation;
	private Animation outToLeftAnimation;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_card);

		Intent intent = getIntent();
		tangochoId = intent.getIntExtra("tangochoId", -1);

		frontFlag = true;
		index = 0;

		// TODO DI
		findCardService = new FindCardServiceImpl(getApplicationContext());
		removeCardService = new RemoveCardServiceImpl(getApplicationContext());

		setAnimations();

		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
	}

	@Override
	public void onResume() {
		super.onResume();

		cardList = findCardService.findCardList(tangochoId);

		view();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		// XMLからメニュー定義を取得し、メニューに設定
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case R.id.add_card:
			intent = new Intent(getApplicationContext(),
					AddCardActivity.class);

			// TODO Tangocho　自体を渡す
			intent.putExtra("tangochoId", tangochoId);

			startActivityForResult(intent, 1);
			break;
		case R.id.remove_card:
			if (cardList.size() < 1) {
				break;
			}

			new AlertDialog.Builder(this)
			.setMessage("カードを削除しますか?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				removeCardService.removeCard(cardList.get(index).getId());
				cardList.remove(index);
				Toast.makeText(getApplicationContext(), "削除しました", Toast.LENGTH_SHORT).show();
				view();
			}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			/* ここにNOの処理 */
			}
			})
			.show();
			break;
		case R.id.edit_card:
			if (cardList.size() < 1) {
				break;
			}
			intent = new Intent(getApplicationContext(),
					EditCardActivity.class);

			// TODO Card　自体を渡す
			Card card = cardList.get(index);
			intent.putExtra("cardId", card.getId());
			intent.putExtra("frontText", card.getFrontText());
			intent.putExtra("backText", card.getBackText());

			startActivityForResult(intent, 1);
			break;
		default:
			// 処理なし
			break;
		}
		return true;
	}

	private void view() {

		TextView cardTextView = (TextView) findViewById(R.id.card);
		TextView cardSideTextView = (TextView) findViewById(R.id.card_side_textview);
		TextView cardIndexTextView = (TextView) findViewById(R.id.card_index);
		TextView cardTextView2 = (TextView) findViewById(R.id.card_2);
		TextView cardSideTextView2 = (TextView) findViewById(R.id.card_side_textview_2);
		TextView cardIndexTextView2 = (TextView) findViewById(R.id.card_index_2);

		Button beforeButton = (Button) findViewById(R.id.before_button);
		Button nextButton = (Button) findViewById(R.id.next_button);
		Button reverseButton = (Button) findViewById(R.id.reverse_button);

		if (cardList.size() != 0) {

			if (index >= cardList.size()) {
				index = cardList.size() - 1;
			}

			Card card = cardList.get(index);
			String text = frontFlag ? card.getFrontText() : card.getBackText();

			if (flipperFlag) {
				cardTextView.setText(text);
				cardSideTextView.setText(frontFlag ? "表" : "裏");
				cardIndexTextView.setText(index + 1 + "/" + cardList.size());
			} else {
				cardTextView2.setText(text);
				cardSideTextView2.setText(frontFlag ? "表" : "裏");
				cardIndexTextView2.setText(index + 1 + "/" + cardList.size());
			}

			beforeButton.setClickable(true);
			nextButton.setClickable(true);
			reverseButton.setClickable(true);
		} else {
			// TODO 暫定デフォルト表示
			cardTextView.setText("カードを追加して下さい。");
			cardSideTextView.setText("");
			cardIndexTextView.setText("0/0");
			cardTextView2.setText("カードを追加して下さい。");
			cardSideTextView2.setText("");
			cardIndexTextView2.setText("0/0");

			beforeButton.setClickable(false);
			nextButton.setClickable(false);
			reverseButton.setClickable(false);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) { // TODO スライド
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_CANCEL:
			break;
		}

		return super.onTouchEvent(event);
	}

	public void before(View view) {
		frontFlag = true;

		if (index <= 0) {
			index = cardList.size() - 1;
		} else {
			index--;
		}

		flipperFlag = !flipperFlag;
		view();

		viewFlipper.setInAnimation(inFromLeftAnimation);
		viewFlipper.setOutAnimation(outToRightAnimation);
		viewFlipper.showPrevious();

	}

	public void next(View view) {
		frontFlag = true;

		if (index < cardList.size() - 1) {
			index++;
		} else {
			index = 0;
		}

		flipperFlag = !flipperFlag;
		view();

		viewFlipper.setInAnimation(inFromRightAnimation);
		viewFlipper.setOutAnimation(outToLeftAnimation);
		viewFlipper.showNext();

	}

	public void reverse(View view) {
		frontFlag = frontFlag ? false : true;

		view();
	}

	private void setAnimations() {
		inFromRightAnimation = AnimationUtils.loadAnimation(this,
				R.anim.right_in);
		inFromLeftAnimation = AnimationUtils
				.loadAnimation(this, R.anim.left_in);
		outToRightAnimation = AnimationUtils.loadAnimation(this,
				R.anim.right_out);
		outToLeftAnimation = AnimationUtils
				.loadAnimation(this, R.anim.left_out);
	}
}
